package dbAccess;

import java.io.*;
import java.util.*;
import java.sql.*;

public class Visit {

	private String defaultStr="unknown";
	private int visitId;
	private int appointmentId;
	private String Diagnosis;
	private String Prescription;
	private String Dosage;
	private String Comments;
	
	private String statusMsg;
	private String creator;
	
	public Visit(String creatorIn)
	{
		visitId=-1;
		appointmentId=-1;
		Diagnosis=defaultStr;
		Prescription=defaultStr;
		Dosage=defaultStr;
		Comments=defaultStr;
		
		statusMsg="";
		creator = creatorIn;
	}

	public boolean CreateVisit(String action)
	{
		this.setStatusMsg("");
		DbAccess access = new DbAccess();
		String query;
		if(action.equals("update"))
		{
			int visitId = this.getVisitId();
			String updateQuery = "update tbl_visit set IsDeleted = '1' where VisitId = " + visitId ;
			access.ExecuteUpdate(access.connect(), updateQuery);
			Login.Log(creator, "update tbl_visit set IsDeleted = '1' where VisitId = " + visitId );
		}
		
		this.setStatusMsg("");
		
		//insert into tbl_visit
		query = "INSERT INTO tbl_visit (AppointmentId, Diagnosis, Prescription, Dosage, Comments, IsDeleted) ";
		query += "VALUES(" + this.getAppointmentId() + ", '" + this.getDiagnosis() + "', '" + this.getPrescription() + "', '";
		query += this.getDosage() + "', '" + this.getComments() + "', '0')";
		if(!access.ExecuteUpdate(access.connect(), query))
		{
			this.setStatusMsg("Could not update tbl_visit with new visit information.");
			return false;
		}
		else
		{
			Login.Log(creator, query);
		}
		
		if (!this.evalVisitId())
		{
			return false;
		}
		
		if (action.equals("update"))
		{
			this.setStatusMsg("Visitation Record updated with new Visit Id: " + this.getVisitId());			
		}
		else
		{
			this.setStatusMsg("Visitation Record created with Visit Id: " + this.getVisitId());
		}
		return true;
	}
	
	private Boolean evalVisitId()
	{
		DbAccess access = new DbAccess();
		String query = "select tbl_visit.VisitId from tbl_visit where tbl_visit.AppointmentId = " + this.appointmentId + " and ";
		query += "tbl_visit.IsDeleted = 0";
		
		try
		{
			ResultSet rs = access.ExecuteSelect(access.connect(), query);
			rs.next();
			this.visitId = rs.getInt("tbl_visit.VisitId");
			return true;
		}
		catch (SQLException e)
		{
			return false;
		}
	}
		
	public int getVisitId() {
		return visitId;
	}

	public void setVisitId(int visitId) {
		this.visitId = visitId;
	}

	public int getAppointmentId() {
		return appointmentId;
	}

	public void setAppointmentId(int appointmentId) {
		this.appointmentId = appointmentId;
	}

	public String getDiagnosis() {
		return Diagnosis;
	}

	public void setDiagnosis(String diagnosis) {
		Diagnosis = diagnosis;
	}

	public String getPrescription() {
		return Prescription;
	}

	public void setPrescription(String prescription) {
		Prescription = prescription;
	}

	public String getDosage() {
		return Dosage;
	}

	public void setDosage(String dosage) {
		Dosage = dosage;
	}

	public String getComments() {
		return Comments;
	}

	public void setComments(String comments) {
		Comments = comments;
	}

	public String getStatusMsg() {
		return statusMsg;
	}

	public void setStatusMsg(String statusMsg) {
		this.statusMsg = statusMsg;
	}
}
